package cn.cttic.app.service.impl;

import cn.cttic.order.api.domain.BaseInstallOrderProcess;
import cn.cttic.order.api.domain.bo.BaseInstallOrderProcessBo;
import cn.cttic.order.api.domain.vo.BaseInstallOrderProcessVo;
import cn.hutool.core.bean.BeanUtil;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.cttic.app.mapper.BaseInstallOrderProcessMapper;
import cn.cttic.app.service.IBaseInstallOrderProcessService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BaseInstallOrderProcessServiceImpl implements IBaseInstallOrderProcessService {

    private final BaseInstallOrderProcessMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public BaseInstallOrderProcessVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public String getOrderImei(String orderId) {
        List<BaseInstallOrderProcess> list = baseMapper.selectList(
            new LambdaQueryWrapper<BaseInstallOrderProcess>().eq(BaseInstallOrderProcess::getInstallOrderId, orderId)
        );
        log.info("查询到数据列表： {}", list);
        for (BaseInstallOrderProcess process : list) {
            if (process.getStepId().equals("10010") && StrUtil.isNotBlank(process.getInteriorData())) {
                log.info("订单： {} IMEI是 {}", orderId, process.getInteriorData());
                return process.getInteriorData();
            }
        }
        log.info("订单： {} 无IMEI", orderId);
        return null;
    }

    @Override
    public String getOrderIdByImei(String imei) {
        List<BaseInstallOrderProcess> list = baseMapper.selectList(
            new LambdaQueryWrapper<BaseInstallOrderProcess>().eq(BaseInstallOrderProcess::getInteriorData, imei)
                .orderByDesc(BaseInstallOrderProcess::getCreateTime)
        );
        log.info("查询到数据列表： {}", list);
        for (BaseInstallOrderProcess process : list) {
            if (StrUtil.isNotBlank(process.getInstallOrderId())) {
                return process.getInstallOrderId();
            }
        }
        log.error("IMEI {} 未找到匹配订单", imei);
        return null;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<BaseInstallOrderProcessVo> queryPageList(BaseInstallOrderProcessBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseInstallOrderProcess> lqw = buildQueryWrapper(bo);
        Page<BaseInstallOrderProcessVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<BaseInstallOrderProcessVo> queryList(BaseInstallOrderProcessBo bo) {
        LambdaQueryWrapper<BaseInstallOrderProcess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseInstallOrderProcess> buildQueryWrapper(BaseInstallOrderProcessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseInstallOrderProcess> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseInstallOrderProcess::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getInstallOrderId()), BaseInstallOrderProcess::getInstallOrderId, bo.getInstallOrderId());
        lqw.eq(bo.getStepIndex() != null, BaseInstallOrderProcess::getStepIndex, bo.getStepIndex());
        lqw.like(StringUtils.isNotBlank(bo.getStepGroupName()), BaseInstallOrderProcess::getStepGroupName, bo.getStepGroupName());
        lqw.like(StringUtils.isNotBlank(bo.getStepName()), BaseInstallOrderProcess::getStepName, bo.getStepName());
        lqw.eq(StringUtils.isNotBlank(bo.getStepId()), BaseInstallOrderProcess::getStepId, bo.getStepId());
        lqw.eq(StringUtils.isNotBlank(bo.getOssId()), BaseInstallOrderProcess::getOssId, bo.getOssId());
        lqw.eq(StringUtils.isNotBlank(bo.getOssUrl()), BaseInstallOrderProcess::getOssUrl, bo.getOssUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getInteriorData()), BaseInstallOrderProcess::getInteriorData, bo.getInteriorData());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(BaseInstallOrderProcessBo bo) {
        BaseInstallOrderProcess add = BeanUtil.toBean(bo, BaseInstallOrderProcess.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(BaseInstallOrderProcessBo bo) {
        BaseInstallOrderProcess update = BeanUtil.toBean(bo, BaseInstallOrderProcess.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseInstallOrderProcess entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
